package com.handinfo.android.ui.window;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWExternalDraw;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.utils.Tools;

/**
 * 
 * 战力动画显示类
 * 
 * @author mujianchen
 * 
 */
public class UIZhanLiAnim implements IUIWindows
{
	public DWFrame m_zlFrame = null;
	public DWBackground m_zlBackground = null;

	public boolean m_open_state;//战力动画窗口是否已打开

	public Bitmap m_zlBitmap_Num = null;//动画所需数字
	public Bitmap m_zlBitmap_ZhanLi = null;//战力文字

	/** 数字拼接 */
	public DWImageBox[] m_ImageBox_num = null;

	public long m_zhanli_loading;//初始战力值
	public long m_zhanli_now;//最新战力值

	public DWGrid m_zlGrid[] = new DWGrid[8];//对齐用

	String m_str;

	public void zhanliAnin()
	{
		m_zlFrame = new DWFrame((DWGameManager.Screen_Width - m_zlBitmap_ZhanLi.getWidth()) / 2,
				DWGameManager.Screen_Height / 8 * 5, m_zlBitmap_ZhanLi.getWidth(), m_zlBitmap_ZhanLi.getHeight());
		m_zlFrame.setTop(true);
		m_zlFrame.setClickClose(false);
		m_zlFrame.setThrough(true);
		m_zlFrame.setTouch(false);

		m_zlBackground = new DWBackground(m_zlBitmap_ZhanLi, m_zlFrame.getShowWidth(), m_zlFrame.getShowHeight());
		m_zlBackground.setNearAnchor(m_zlFrame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);

		for (int i = 0; i < m_zlGrid.length; i++) {
			m_zlGrid[i] = new DWGrid((m_zlFrame.getShowWidth() / 8) * i, 0, m_zlFrame.getShowWidth() / 8, m_zlFrame
					.getShowHeight() / 3 * 2);
			m_zlGrid[i].setNearAnchor(m_zlFrame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM,
					(m_zlFrame.getShowWidth() / 8) * i, 0);
		}

		for (int i = 0; i < m_str.length(); i++) {
			m_ImageBox_num[i] = new DWImageBox(m_zlBitmap_Num.getWidth() / 10, m_zlBitmap_Num.getHeight());
		}

		if (m_str.length() == 1 || m_str.length() == 2) {
			m_ImageBox_num[0].setNearAnchor(m_zlGrid[3], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER);
		}
		else if (m_str.length() == 3 || m_str.length() == 4) {
			m_ImageBox_num[0].setNearAnchor(m_zlGrid[2], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER);
		}
		else if (m_str.length() == 5 || m_str.length() == 6) {
			m_ImageBox_num[0].setNearAnchor(m_zlGrid[1], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER);
		}

		if (m_str.length() > 1) {
			for (int i = 1; i < m_str.length(); i++) {
				m_ImageBox_num[i].setNearAnchor(m_ImageBox_num[i - 1], Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER);
			}
		}

		m_zlFrame.addControl(m_zlBackground);
		for (int i = 0; i < m_zlGrid.length; i++) {
			m_zlFrame.addControl(m_zlGrid[i]);
		}
		for (int i = 0; i < m_str.length(); i++) {
			m_zlFrame.addControl(m_ImageBox_num[i]);
		}
		DWControlsManager.getInstance().addControl(m_zlFrame);
	}

	public void init()
	{
		m_zlBitmap_Num = UIWindows.createImage("/img/newui/shuzi_8.gnp");//数字拼接
		m_zlBitmap_ZhanLi = UIWindows.createImage("/img/newui/zhanli_1.gnp");//战力
	}

	/**
	 * 2秒钟只能第一次打开此界面
	 */
	public boolean m_frist_open_state = false;

	/**
	 * 关闭其他界面时判断战力是否改变
	 * 
	 * 如改变则打开战力动画界面
	 */
	public void open_other(long zhanli_num)
	{
		if (zhanli_num != m_zhanli_loading) {
			if (m_frist_open_state == false) {
				m_str = String.valueOf(zhanli_num);
				m_ImageBox_num = new DWImageBox[m_str.length()];
				zhanli = new int[m_str.length()];
				open((byte) 0);
				m_frist_open_state = true;
			}
			m_zhanli_now = zhanli_num;
		}
	}

	public void open(byte index)
	{
		if (m_open_state == false) {
			m_open_state = true;
			zhanliAnin();
			long nowtime = System.currentTimeMillis();
			doTime(nowtime + 2500);//---------->2.5秒结束
			Tools.debugPrintln("mujianchen------>打开战力动画界面");
		}
	}

	/**
	 * 动画开启计时
	 */
	public void doTime(final long time)
	{
		new Thread(new Runnable() {

			public void run()
			{
				while (true) {
					long nowtime = System.currentTimeMillis();
					if (nowtime >= time) {
						close((byte) 0);
						break;
					}
					else {
						Thread.yield();
					}
				}
			}
		}).start();
	}

	public void close(byte index)
	{
		m_open_state = false;
		m_logic_state = false;
		m_zhanli_loading = DWGameManager.getInstance().m_role.m_zhanli;
		for (int i = 0; i < m_str.length(); i++) {
			zhanli[i] = 0;
		}
		m_frist_open_state = false;
		DWControlsManager.getInstance().removeControl(m_zlFrame);
	}

	public void dispose()
	{

	}

	/**
	 * 
	 * 10的n次幂
	 * 
	 * @param n
	 * @return
	 */
	public int getSquare(int n)
	{
		int num = 1;
		int a = 10;

		int i = n;
		while (i > 0) {
			if (i % 2 == 0) {
				a = a * a;
				i = i / 2;
			}
			else {
				num = num * a;
				i--;
			}
		}
		return num;
	}

	public int[] zhanli;//下标从个位开始算
	public boolean m_logic_state = false;//是否第一次进入logic

	public void logic()
	{
		if (m_open_state == true) {
			for (int i = 0; i < m_str.length(); i++) {
				final int index = i;
				if (m_logic_state == false) {
					zhanli[index] = (int) (m_zhanli_loading / getSquare(index) % 10);
				}
				m_ImageBox_num[(m_str.length() - 1) - index].addExternalDraw(new DWExternalDraw() {

					public void externalDraw(DWGraphics g, int x, int y)
					{
						Tools.drawNumber(g, m_zlBitmap_Num, x, y, m_zlBitmap_Num.getWidth() / 10,
								m_zlBitmap_Num.getHeight(), 1, 1, zhanli[index], 1, Tools.LEFT_VCENTER, false);
					}
				});
				if (zhanli[index] != m_zhanli_now / getSquare(index) % 10) {
					zhanli[index]++;
					if (zhanli[index] > 9) {
						zhanli[index] = 0;
					}
				}
				if (zhanli[index] == m_zhanli_now / getSquare(index) % 10) {
					m_ImageBox_num[(m_str.length() - 1) - index].addExternalDraw(new DWExternalDraw() {

						public void externalDraw(DWGraphics g, int x, int y)
						{
							Tools.drawNumber(g, m_zlBitmap_Num, x, y, m_zlBitmap_Num.getWidth() / 10,
									m_zlBitmap_Num.getHeight(), 1, 1, m_zhanli_now / getSquare(index) % 10, 1,
									Tools.LEFT_VCENTER, false);
						}
					});
				}
			}
			m_logic_state = true;
		}
	}
}
